package com.xiangxiangjd.controller;

import cn.afterturn.easypoi.word.WordExportUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xiangxiangjd.controller.helper.$;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangxiangjd.service.ManagementDateNoService;
import com.xiangxiangjd.bean.ManagementDateNo;
import com.xiangxiangjd.controller.model.ApiResultModel;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处置批次 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-11-23
 */
@Controller
@RequestMapping("/api/managementDateNo")
@Api(tags = "处置批次")
public class ManagementDateNoController extends BaseController{




    /** 分页查询ManagementDateNo
     * @param page  页码
     * @param limit  查几条
     * @param managementDateNo  前端传进的参数
     * @return 返回统一分页信息
     */
    @PostMapping(value = "admin/findManagementDateNoByPage")
    @ResponseBody
    @ApiOperation(value = "查询-处置订单", notes = "查询-处置订单")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ResultPage findManagementDateNoByPage(Integer page, Integer limit,ManagementDateNo managementDateNo){
             return managementDateNoService.findManagementDateNoByPage(page,limit,managementDateNo);
    }

    /** 添加ManagementDateNo
     * @param managementDateNo  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "/addManagementDateNo")
    @ResponseBody
    @ApiOperation(value = "添加一批处置", notes = "添加一批处置")
    @PreAuthorize("hasRole('ROLE_USER')")
    public ApiResultModel saveManagementDateNo(@RequestBody ManagementDateNo managementDateNo  ){

             return managementDateNoService.addManagementDateNo(managementDateNo);
    }

    /** 编辑ManagementDateNo
     * @param managementDateNo  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/editManagementDateNo")
    @ResponseBody
    public ApiResultModel editManagementDateNo(ManagementDateNo managementDateNo  ){
             return managementDateNoService.editManagementDateNo(managementDateNo);
    }

    /** 删除ManagementDateNo
     * @param ids  前端传进的ID
     * @return 返回统一信息
     */
    @PostMapping(value = "admin/delManagementDateNo")
    @ResponseBody
    public ApiResultModel delManagementDateNo(@RequestParam("datas") ArrayList ids){


             return managementDateNoService.delManagementDateNos(ids);
    }



    /**
     * 处置审批通过
     * @param managementDateNo  前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "/managementApproved")
    @ResponseBody
    @ApiOperation(value = "处置审批通过", notes = "处置审批通过")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel managementApproved(Integer managementDateNoId  ){

        return managementDateNoService.managementApproved(managementDateNoId,getUserInfo().getId());
    }

    /**
     * 审批不通过
     * @param   前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "/managementFail")
    @ResponseBody
    @ApiOperation(value = "处置审批不通过", notes = "处置审批不通过")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel managementFail(Integer managementDateNoId,String  managementFail){

        boolean update = managementDateNoService.update(new LambdaUpdateWrapper<ManagementDateNo>()
                .set(ManagementDateNo::getManagementState, "3")
                .set(ManagementDateNo::getManagementFail, managementFail)
                .eq(ManagementDateNo::getId, managementDateNoId));
        if (!update){
            return  $.error("失败");
        }


    return $.success("成功");
    }


    /**
     * 审批不通过
     * @param   前端传进的参数
     * @return 返回统一信息
     */
    @PostMapping(value = "/getByManagement")
    @ResponseBody
    @ApiOperation(value = "查看订单详情", notes = "查看订单详情")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ApiResultModel getByManagement(@RequestParam("managementDateNoId") Integer managementDateNoId){

        ManagementDateNo managementDateNo=managementDateNoService.getByManagementAllGoods(managementDateNoId);

    return $.success(managementDateNo);
    }


    /**
     * 处置报告表
     */
    @GetMapping(value = "/improt")
    @ApiOperation(value = "处置 word 导出", notes = "处置 word 导出")
  //  @PreAuthorize("hasRole('ROLE_USER')")
    public void downDocManagementDateNo(@RequestParam("managementDateNoId") Integer managementDateNoId) throws Exception {


        Map map=managementDateNoService.downDocManagementDateNo(managementDateNoId);


        XWPFDocument doc = WordExportUtil.exportWord07("word/ManagementDocx.docx", map);

        doc.enforceReadonlyProtection();

        String fileName = System.currentTimeMillis() + ".docx";
        HttpServletResponse response = getResponse();

        response.reset();
        response.setContentType("application/msword");
        String dispositionValue = "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", dispositionValue);
        response.setCharacterEncoding("UTF-8");

        OutputStream output = response.getOutputStream();
        doc.write(output);
        output.close();

    }


}
